package com.hanbang.maige.view.aboutme.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hanbang.maige.R;
import com.hanbang.maige.code.base.view.activity.BaseMvpActivity;
import com.hanbang.maige.code.broadcast.BroadcastUtils;
import com.hanbang.maige.mode.javabean.aboutme.MyInfoData;
import com.hanbang.maige.mode.javabean.base.UserData;
import com.hanbang.maige.pressenter.aboutme.GeRenZiLiaoPresenter;
import com.hanbang.maige.utils.animator.AnimUtils;
import com.hanbang.maige.utils.bitmap.GlideOptions;
import com.hanbang.maige.utils.bitmap.GlideUtils;
import com.hanbang.maige.utils.other.StringUtils;
import com.hanbang.maige.view.aboutme.IAboutMeBaseView;
import com.hanbang.maige.view.login.activity.BiaoQianActivity;
import com.hanbang.maige.widget.button.FlatButton;
import com.hubng.photo_hander.PhotoHander;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

import static com.hanbang.maige.code.MyApplication.myApp;
import static com.hanbang.maige.mode.javabean.base.UserData.userData;

/**
 * Created by 方朝辉 on 2016/10/26.
 */

public class GeRenZiLiaoActivity extends BaseMvpActivity<IAboutMeBaseView.IGeRenZiLiaoView, GeRenZiLiaoPresenter> implements IAboutMeBaseView.IGeRenZiLiaoView {
    @BindView(R.id.wanshanTv)
    TextView wanshanTv;
    @BindView(R.id.headIv)
    ImageView headIv;
    @BindView(R.id.unameTv)
    TextView unameTv;
    @BindView(R.id.uphoneTv)
    TextView uphoneTv;
    @BindView(R.id.uemailTv)
    TextView uemailTv;
    @BindView(R.id.uaddrTv)
    TextView uaddrTv;
    @BindView(R.id.ubbiaoqianTv)
    TextView ubbiaoqianTv;
    @BindView(R.id.touxiangLay)
    LinearLayout touxiangLay;
    @BindView(R.id.unameLay)
    LinearLayout unameLay;
    @BindView(R.id.phoneLay)
    LinearLayout phoneLay;
    @BindView(R.id.emailLay)
    LinearLayout emailLay;
    @BindView(R.id.addrLay)
    LinearLayout addrLay;
    @BindView(R.id.biaoqianLay)
    LinearLayout biaoqianLay;
    MyInfoData myInfoData = new MyInfoData();
    @BindView(R.id.submitBc)
    FlatButton submitBc;

    @Override
    public GeRenZiLiaoPresenter initPressenter() {
        return new GeRenZiLiaoPresenter();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_gerenxinxi;
    }


    public static void startUI(Context context) {
        Intent intent = new Intent(context, GeRenZiLiaoActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        toolbar.setBack(this);
        toolbar.addAction(0, "个人信息", R.drawable.material_xiangji);
        String wanShanStr = wanshanTv.getText().toString();
        SpannableString ss = new SpannableString(wanShanStr);
        ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.main_color)), 8, ss.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(1.1f), 8, ss.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        wanshanTv.setText("");
        wanshanTv.append(ss);

    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public void clearData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        upStaticData();
    }


    @OnClick({ R.id.wanshanTv, R.id.touxiangLay, R.id.unameLay, R.id.phoneLay, R.id.emailLay, R.id.addrLay, R.id.biaoqianLay, R.id.submitBc})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.touxiangLay:
                PhotoHander.create()
                        .single()
                        .crop(1, 1)
                        .showCamera(true)
                        .compress(true)
                        .showCircle(true)
                        .start(GeRenZiLiaoActivity.this, REQUEST_CODE + 1);
                break;
            case R.id.unameLay:
                MaterialDialog dialogn = new MaterialDialog.Builder(this)
                        .title("姓名")
                        .input("", unameTv.getText(), true, new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                                unameTv.setText(input.toString());
                            }
                        })
                        .positiveText("确定")
                        .negativeText("取消")
                        .negativeColorRes(R.color.gray_text)
                        .build();
                dialogn.show();

                break;
            case R.id.phoneLay:
                break;
            case R.id.emailLay:
                MaterialDialog dialoge = new MaterialDialog.Builder(this)
                        .title("邮箱")
                        .input("", uemailTv.getText(), true, new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                                uemailTv.setText(input.toString());
                            }
                        })
                        .positiveText("确定")
                        .negativeText("取消")
                        .negativeColorRes(R.color.gray_text)
                        .build();
                dialoge.show();
                break;
            case R.id.addrLay:
                break;
            case R.id.biaoqianLay:
                BiaoQianActivity.startUI(this);
                break;
            case R.id.submitBc:
                myInfoData.setNiChenName(unameTv.getText().toString());
                myInfoData.setEmail(uemailTv.getText().toString());
                if (StringUtils.isEmpty(myInfoData.touxiangPath)) {
                    AnimUtils.shakeLeft(touxiangLay, 0.85f, 6f);
                    showWarningSnackbar("请选择一张头像");
                    return;
                }
                if (StringUtils.isEmpty(unameTv.getText().toString())) {
                    AnimUtils.shakeLeft(unameLay, 0.85f, 6f);
                    showWarningSnackbar("请填写姓名");
                    return;
                }
                if (StringUtils.isEmpty(uemailTv.getText().toString())) {
                    AnimUtils.shakeLeft(unameLay, 0.85f, 6f);
                    showWarningSnackbar("请填写邮箱");
                    return;
                }
                if (StringUtils.isEmpty(myInfoData.getBiaoqianData().getHyValueName())) {
                    AnimUtils.shakeLeft(unameLay, 0.85f, 6f);
                    showWarningSnackbar("请选择标签");
                    return;
                }
                presenter.updateUser(myInfoData);
                break;
            case R.id.wanshanTv:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE + 1 && resultCode == RESULT_OK) {
            List<String> mSelectPath = data.getStringArrayListExtra(PhotoHander.EXTRA_RESULT);
            if (mSelectPath != null && mSelectPath.size() != 0) {
                myInfoData.touxiangPath = mSelectPath.get(0);
                GlideUtils.show(headIv, myInfoData.touxiangPath, new GlideOptions.Builder()
                        .setPlaceholder1_1()
                        .addTransformation(new CropCircleTransformation(myApp))
                        .bulider());
            }
        }
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            myInfoData.biaoqianData = data.getParcelableExtra("data");
            ubbiaoqianTv.setText(myInfoData.biaoqianData.getHyValueName());
        }

    }

    private void upStaticData() {
        if (UserData.getUserData() != null) {
            GlideUtils.show(headIv, userData.getHeadPhotoSma(), new GlideOptions.Builder()
                    .setPlaceholder1_1()
                    .addTransformation(new CropCircleTransformation(myApp))
                    .bulider());
            unameTv.setText(userData.getNiChenName());
            uphoneTv.setText(userData.getTelPhone());
            uemailTv.setText(userData.getEmail());
            ubbiaoqianTv.setText(userData.getHyValueName());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void complete() {
        sendBroadcast(new Intent(BroadcastUtils.GERENXINXI_BAOCUN));
        finish();
    }
}
